<?php
App::uses('AppController', 'Controller');
/**
 * Recipes Controller
 *
 * @property Recipe $Recipe
 * @property PaginatorComponent $Paginator
 * @property nComponent $n
 * @property SessionComponent $Session
 */
class RecipesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');
	var $uses = array('Recipe', 'RecipeCategory');
    
    var $paginate = array(
			'limit' => 15,
			'order' => array(
				'Recipe.created_date' => 'DESC'			
			),
		);
/**
 * index method
 *
 * @return void
 */
	public function index() {
        $this->Paginator->settings = $this->paginate;
        
		$this->Recipe->recursive = -1;
		$this->set('recipes', $this->Paginator->paginate());
        //$this->set('recipes', $this->Recipe->find('all'));
        
        $this->RecipeCategory->recursive = -1;
        $this->set('recipeCategory', $this->RecipeCategory->find('all'));
	}
    
    public function category($id) {
        $this->paginate['conditions'] = array('Recipe.recipe_category_id' => $id);
        $this->Paginator->settings = $this->paginate;
        
		$this->Recipe->recursive = -1;
		$this->set('recipes', $this->Paginator->paginate());
        
        $this->RecipeCategory->recursive = -1;
        $this->set('categoryCurrent', $this->RecipeCategory->findById($id));
        
        $this->RecipeCategory->recursive = -1;
        $this->set('recipeCategory', $this->RecipeCategory->find('all'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Recipe->exists($id)) {
			throw new NotFoundException(__('Invalid recipe'));
		}
		$options = array('conditions' => array('Recipe.' . $this->Recipe->primaryKey => $id));
		$recipe = $this->Recipe->find('first', $options);
        $this->set('recipe', $recipe);
        
        $this->RecipeCategory->recursive = -1;
        $this->set('recipeCategory', $this->RecipeCategory->find('all'));
        
        $this->Recipe->recursive = -1;
        if (!empty($recipe['Recipe']['relation'])) {
            $this->set('recipeSame', $this->Recipe->find('all', array('conditions'=>array('recipe_id <>' => $id, 'OR' => array('recipe_category_id' => $recipe['Recipe']['recipe_category_id'], 'relation' => $recipe['Recipe']['relation'])))));
        } else {
           $this->set('recipeSame', $this->Recipe->find('all', 
            array(
                'conditions'=>array('recipe_id <>' => $id, 'recipe_category_id' => $recipe['Recipe']['recipe_category_id']),
                'limit' => 3,
                'order' => 'created_date DESC'
            )
           ));
        }
	}
}
